package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.vcc.core.annotations.RepeatSubmit;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.AdminParam;
import com.vcc.service.boss.request.adminParam.BossAdminParamCreateRequest;
import com.vcc.service.boss.request.adminParam.BossAdminParamUpdateRequest;
import com.vcc.service.boss.response.adminParam.AdminParamVoBoss;
import com.vcc.service.service.AdminParamService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("adminParam")
public class AdminParamController {

    @Resource
    private AdminParamService adminParamService;


    @RestPack
    @SaCheckLogin
    @SaCheckPermission(value = "admin:param:list:view")
    @GetMapping("paging")
    public Page<AdminParamVoBoss> paging(
            Integer pageNumber,
            Integer pageSize,
            String code,
            String name
    ) {
        return adminParamService.paging(
                pageNumber != null ? pageNumber : 1,
                pageSize != null ? pageSize : 20,
                code,
                name
        );
    }


    @RestPack
    @SaCheckLogin
    @SaCheckPermission(value = "admin:param:list:create")
    @PostMapping("create")
    @RepeatSubmit
    public boolean create(@RequestBody @Validated BossAdminParamCreateRequest bossAdminParamCreateRequest) {
        if (adminParamService.existsByCode(bossAdminParamCreateRequest.getCode())) {
            throw new ProgramException("参数代码已存在");
        }
        AdminParam adminParam = new AdminParam();
        adminParam.setName(bossAdminParamCreateRequest.getName());
        adminParam.setCode(bossAdminParamCreateRequest.getCode());
        adminParam.setValue(bossAdminParamCreateRequest.getValue());
        return adminParamService.save(adminParam);
    }


    @RestPack
    @SaCheckLogin
    @SaCheckPermission(value = "admin:param:list:edit")
    @PostMapping("update")
    @RepeatSubmit
    public boolean update(@RequestBody @Validated BossAdminParamUpdateRequest bossAdminParamUpdateRequest) {
        return adminParamService.update(bossAdminParamUpdateRequest);
    }


    @RestPack
    @SaCheckLogin
    @GetMapping("getByCode")
    @RepeatSubmit
    public String getByCode(String code) {
        AdminParam adminParam = adminParamService.getByCode(code);
        return adminParam.getValue();
    }


    @RestPack
    @SaCheckLogin
    @GetMapping("listByCodes")
    @RepeatSubmit
    public List<AdminParamVoBoss> listByCodes(@RequestParam("codes") List<String> codes) {
        List<AdminParam> adminParams = adminParamService.listByCodes(codes);
        return adminParams.stream().map(it -> {
            AdminParamVoBoss adminParamVo = new AdminParamVoBoss();
            adminParamVo.setId(it.getId());
            adminParamVo.setCode(it.getCode());
            adminParamVo.setName(it.getName());
            adminParamVo.setValue(it.getValue());
            return adminParamVo;
        }).toList();
    }
}
